package com.china08.yxyapi.service.yxyresource.statistics.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceAllCountService;
@Service
public class ResourceAllCountServiceImpl implements ResourceAllCountService {
	@Autowired
	private ElasticsearchTemplate et;
	@Override
	public List<Long> statisticsResourceCount(String districtCode, Integer schoolBureauLevel) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder.mustNot(builderType);
		TermQueryBuilder builderTypestauts = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder.must(builderTypestauts);
		SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();
		
		// 查询出来的总数量
		long allCount = et.count(query,ElasticYxyResResource.class);
		List<Long> longCount = new ArrayList<Long>();
		longCount.add(allCount);
		
		
		
		
		BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
		TermQueryBuilder builderTypestauts1 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder1.must(builderTypestauts1);
		TermQueryBuilder builderType3 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder1.mustNot(builderType3);
		TermQueryBuilder builderDel =null;
		if (schoolBureauLevel == 0) {
			builderDel = new TermQueryBuilder("districtCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}else if(schoolBureauLevel == 1){
			builderDel = new TermQueryBuilder("cityCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}else if (schoolBureauLevel == 2) {
			builderDel = new TermQueryBuilder("provinceCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}
		// 统计近30天新增加的资源数量 uploadTime
		Date currentDate = new Date();
		// 30天的毫秒数
		long threeDate = 2592000000L;
//		Date date = new Date(currentDate.getTime() - threeDate);
//      TermQueryBuilder builderDel2 = new TermQueryBuilder("uploadTime",
//      date);
//      boolQueryBuilder.
//
		RangeQueryBuilder rangeDate = new RangeQueryBuilder("uploadTime");
//	    rangeDate.format(date); // 查询格式    （先试下如果不行的话，在加下 格式） 
//		rangeDate.gt(date);	
		rangeDate.gt(currentDate.getTime() - threeDate);
		boolQueryBuilder1.filter(rangeDate);
		
		SearchQuery query3=new NativeSearchQueryBuilder().withQuery(boolQueryBuilder1).build();
		long nearCount = et.count(query3,ElasticYxyResResource.class);
		longCount.add(nearCount);
		return longCount;
	}
	
}
